 var table ,form ,laypage ,layer ,element ,laydate ,upload;//时间组件
var check=true;
$(function(){ 
	var hid=getQueryString("id");
	var saveurl="/switchCabinet/saveSwitchCabinet.do";
	layui.use(['table','form','laypage','layer', 'element', 'laydate','upload'], function(){
		 table = layui.table;
		 form=layui.form;
		 laypage = layui.laypage ;//分页
		 layer = layui.layer ;//弹层
		 element = layui.element; //元素操作
		 laydate = layui.laydate; //时间组件 
		 upload = layui.upload;

        if(hid){//编辑
     	   $("#Title").html("开关柜信息编辑");    	   
     	   saveurl="/switchCabinet/editSwitchCabinet.do";
     	   var questData = {"id": hid};
      	  	$.ajax({
  	           url: "/switchCabinet/getOneSwitchCabinet.do",               //请求地址
  	           type: "POST",              //请求类型
  	           data:formatParam(questData),              //请求数据
  	           timeout: 200000,                //请求超时时间(毫秒)
  	           headers: setHeader(),
  	           success:function(res){         //请求成功
  	        	   if(res.status == 200)
  	  				{
  	        		   	var num=res.resultData;
  	        		    $("#id").val(hid);
  	        		    $("#cabinetNumber").val(num.cabinetNumber);
  	    				$("#name").val(num.name); 	    				  	    				
  	    				$("#stationName").val(num.stationName); 
 	        		    $("#station").val(num.station);
	    				$("#remarks").val(num.remarks);
  	    				form.render();
  	  				}else{          	  				    
  	  					layer.msg(res.resultMsg, {icon: res.icon});
  	  				}
  	  				      
                }
             })
        } 
    	 //表单验证
        form.verify({	  
        	fname: function(value, item)
	          { 
	  			  if(validate.isBlank(value))
	  			  {
	  				 return "请填写名称";
	  			  }
	  			 if(!validate.isLength(value,1,50))
  		      {
  		         return '名称长度不能超过50个字符';
  		      }
	  	  },
	  	fcabinetNumber: function(value, item)
          { 
  			  if(validate.isBlank(value))
  			  {
  				 return "请填写柜号";
  			  }
  	      },
	     fstation: function(value, item)
	        {  
			  if(validate.isBlank(value))
			  {
				 return "请选择所属变电站";
			  }
	     },
         fremarks: function(value, item)
	          {   //备注长度验证
	  			 if(!validate.isLength(value,0,200))
  		      {
  		         return '备注长度不能超过200个字符';
  		      }
	  	   }
		  	          
       })
      form.on('submit(formDemo)', function(data){  
			this.blur();//防止点回车，按钮重复点击
			var loadIndex = layer.load(1);//显示加载层
			//判断时间为空值为null
			var requestData= formToJsonObj("form_edit", []);
			$.ajax({
				url:saveurl,
				type:'POST',
				async:false,
				data:formatParam(requestData),
				timeout: 200000,
				headers: setHeader(),
				success:function(res){         //请求成功
		        	   if(res.status == 200) {  
		        		   layer.msg(res.resultMsg, {icon: res.icon},function(){
		   					     var index = parent.layer.getFrameIndex(window.name);
		   					      parent.layui.table.reload("tabledata")//
		   					      parent.layer.close(index);//关闭弹出的子页面窗口
		   					}); 					
		        	    }else if(res.status == 666){
		  					TIMEOUT(); 
		  		        }else{
		  					layer.msg(res.resultMsg, {icon: res.icon});		  					
		  				}  
		        	   layer.close(loadIndex);//关闭加载层
	              }		
			})
		  return false; //阻止表单跳转。
	  })
   }) 	
})
